import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '../store/auth'

// 前台路由
const routes = [
  {
    path: '/',
    component: () => import('../layouts/FrontLayout.vue'),
    children: [
      {
        path: '',
        name: 'home',
        component: () => import('../views/Home.vue')
      },
      {
        path: 'prompts',
        name: 'prompts',
        component: () => import('../views/PromptList.vue')
      },
      {
        path: 'prompts/create',
        name: 'prompt-create',
        component: () => import('../views/PromptForm.vue')
      },
      {
        path: 'prompts/:id',
        name: 'prompt-detail',
        component: () => import('../views/PromptDetail.vue')
      },
      {
        path: 'prompts/:id/edit',
        name: 'prompt-edit',
        component: () => import('../views/PromptForm.vue')
      },
      {
        path: 'pricing',
        name: 'pricing',
        component: () => import('../views/Pricing.vue')
      },
      {
        path: 'user',
        name: 'user-center',
        component: () => import('../views/Profile.vue'),
        meta: { requiresAuth: true }
      }
    ]
  },
  // 添加直接的登录和注册路由重定向
  {
    path: '/login',
    redirect: '/auth/login'
  },
  {
    path: '/register',
    redirect: '/auth/register'
  },
  {
    path: '/auth',
    component: () => import('../layouts/AuthLayout.vue'),
    children: [
      {
        path: 'login',
        name: 'login',
        component: () => import('../views/auth/Login.vue')
      },
      {
        path: 'register',
        name: 'register',
        component: () => import('../views/auth/Register.vue')
      }
    ]
  },
  // 后台管理路由
  {
    path: '/admin',
    component: () => import('../layouts/AdminLayout.vue'),
    meta: { requiresAdmin: true },
    children: [
      {
        path: '',
        name: 'admin-dashboard',
        component: () => import('../views/admin/AdminDashboard.vue')
      },
      {
        path: 'users',
        name: 'admin-users',
        component: () => import('../views/admin/UserManagement.vue')
      },
      {
        path: 'plans',
        name: 'admin-plans',
        component: () => import('../views/admin/PricingPlanManagement.vue')
      },
      {
        path: 'admins',
        name: 'admin-admins',
        component: () => import('../views/admin/AdminManagement.vue')
      }
    ]
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'not-found',
    component: () => import('../views/NotFound.vue')
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

// 路由守卫
router.beforeEach(async (to, from, next) => {
  const authStore = useAuthStore()
  
  // 如果有token但没有用户信息，先获取用户信息
  if (authStore.token && !authStore.user) {
    try {
      await authStore.fetchCurrentUser()
    } catch (error) {
      // 如果获取用户信息失败，清除token
      authStore.logoutUser()
    }
  }

  if (to.meta.requiresAuth && !authStore.isAuthenticated) {
    next({ name: 'login', query: { redirect: to.fullPath } })
  } else if (to.meta.requiresAdmin && (!authStore.isAuthenticated || !authStore.isAdmin)) {
    next({ name: 'login', query: { redirect: to.fullPath } })
  } else {
    next()
  }
})

export default router